#include "AllBehavior.h"
#include <stdlib.h>
#include <ctime>
#include "../Character.h"
#include "../Kinematic.h"
#include "../Steering.h"
#include "Waiting.h"

Waiting::Waiting(Character* character,vector<Character* >* _characters,vector<Obstacle *>* _obstacle){
  arrive = false;
  float aux[3];
  aux[0] = 90*sin(character->getKinematic()->orientation);
  aux[1] = 90*cos(character->getKinematic()->orientation);
  Kinematic* k = new Kinematic();
  sumarVectores(character->getKinematic()->position,aux,k->position);
  c = new Character(k);
  //-  Aqui introducimos el comportamiento Nothing en la 0ra. posicion -//
  Nothing * no = new Nothing();
  no->SetCharacter(character);
  no->SetTarget(c);
  set.push_back(no);
  //-  Aqui introducimos el comportamiento Arrive en la 1era. posicion -//
  Arrive *ar = new Arrive();
  ar->SetCharacter(character);
  ar->SetTarget(c);
  set.push_back(ar);
  //-  Aqui introducimos el comportamiento Face en la 2da. posicion -//
  Face *face = new Face();
  face->SetCharacter(character);
  face->SetTarget(c);
  set.push_back(face);
  //-  Aqui introducimos el comportamiento Align en la 3era. posicion -//
  Align *al = new Align();
  al->SetCharacter(character);
  al->SetTarget(c);
  set.push_back(al);
  //-  Aqui introducimos el comportamiento Separation en la 4ta. posicion -//
  Separation * sep = new Separation(_characters,_obstacle);
  sep->SetCharacter(character);
  sep->SetTarget(c);
  set.push_back(sep);
  old_time = SDL_GetTicks();
  srand((unsigned) time(0));
  change =((float) (random()%55))/(random()%7+1);
  current_b = 1;
  arrive = false;
}

Steering* Waiting::getSteering(){
  Uint32 current_time = SDL_GetTicks();
  if (current_time - old_time > 1000){
    arrive = false;
    old_time = current_time;
  }
  if (!arrive){
    if (!(random()%10)){
      Kinematic *k = set[0]->GetCharacter()->getKinematic();
      k->position[0] =k->position[0] + random()%100 -50;
      k->position[1] =k->position[1] + random()%100 -50;
      k->position[2] =k->position[2] + random()%100 -50;
      set[1]->SetTarget(new Character(k));
      arrive = true;
    }else{
      
    }
  }else{
    Kinematic * k = set[1]->GetTarget()->getKinematic();
    Kinematic *myK = set[1]->GetCharacter()->getKinematic();
    float aux[3];
    restarVectores(k->position,myK->position,aux);
    delete k;
    if (length(aux) <  30){
      arrive = false;
    }
  }
  return set[current_b]->getSteering();
}
